#include <nice2d/spritedeletor.h>
#include <nice2d/sprite.h>
#include <nice2d/scene.h>
#include <nice2d/application.h>
#include <nice2d/components/spritestatus.h>

class SpriteDirectDeleter : public SpriteDeleter
{
public:
    void setScene(ScenePointer scene) override
    {
        (void)scene;
    }

    bool shouldDelete(SpritePointer sprite)const override
    {
        return sprite && GET_COMPONENT(sprite, SpriteStatus)->getSpriteStatus() == SPRITE_STATUS_DEATH;
    }
};

class SpriteSlopDeleter : public SpriteDeleter
{
public:
    void setScene(ScenePointer inputScene) override
    {
        scene = inputScene;
    }

    bool shouldDelete(SpritePointer sprite)const override
    {
        if (!sprite)
            return false;
        if (scene.expired())
            return false;
        bool ret = !sprite->getBoundingBox().intersects(scene.lock()->getBoundingBox());
        if (ret)
            GET_COMPONENT(sprite, SpriteStatus)->setSpriteStatus(SPRITE_STATUS_DEATH);
        return ret;
    }
private:
    SceneWeakPointer scene;
};

std::shared_ptr<SpriteDeleter> SpriteDeleter::create(SpriteDeleter_Type deleteType)
{
    if (deleteType == SpriteDeleter_Direct)
        return std::make_shared<SpriteDirectDeleter>();
    else if (deleteType == SpriteDeleter_Slop)
        return std::make_shared<SpriteSlopDeleter>();
    return nullptr;
}
